import { MockMethod } from "vite-plugin-mock";

const DEVICE_PROPERTIES = {
  DEFAULT: [
    {
      property: "name",
      displayName: "别名",
      dataType: "string",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "",
      enumValues: null,
      required: true
    },
    {
      property: "memo",
      displayName: "备注",
      dataType: "string",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: null,
      required: false
    },
    // {
    //   property: "skipCheck",
    //   displayName: "skipCheck",
    //   dataType: "boolean",
    //   max: null,
    //   min: null,
    //   precision: null,
    //   unit: null,
    //   defaultValue: true,
    //   enumValues: null,
    //   required: false
    // },
    {
      property: "port",
      displayName: "port",
      dataType: "string",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: null,
      required: false
    }
  ],
  ADAM4118: [
    {
      property: "baudRate",
      displayName: "波特率",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: 9600,
      enumValues: ["9600", "19200", "38400", "57600", "115200"],
      required: true
    },
    {
      property: "dataBits",
      displayName: "数据位",
      dataType: "number",
      max: 8,
      min: 5,
      precision: 0,
      unit: null,
      defaultValue: 8,
      enumValues: null,
      required: true
    },
    {
      property: "stopBits",
      displayName: "停止位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "One",
      enumValues: ["None(无)", "One(1)", "Two(2)", "OnePointFive(1.5)"],
      required: true
    },
    {
      property: "parity",
      displayName: "奇偶校验位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "Odd",
      enumValues: [
        "None(无校验)",
        "Odd(奇校验)",
        "Even(偶校验)",
        "Mark(1校验)",
        "Space(0校验)"
      ],
      required: true
    },
    {
      property: "address",
      displayName: "地址",
      dataType: "number",
      max: 255,
      min: 1,
      precision: 0,
      unit: null,
      defaultValue: 1,
      enumValues: null,
      required: true
    }
  ],
  ADAM4024: [
    {
      property: "baudRate",
      displayName: "波特率",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: 9600,
      enumValues: ["9600", "19200", "38400", "57600", "115200"],
      required: true
    },
    {
      property: "dataBits",
      displayName: "数据位",
      dataType: "number",
      max: 8,
      min: 5,
      precision: 0,
      unit: null,
      defaultValue: 8,
      enumValues: null,
      required: true
    },
    {
      property: "stopBits",
      displayName: "停止位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "One",
      enumValues: ["None(无)", "One(1)", "Two(2)", "OnePointFive(1.5)"],
      required: true
    },
    {
      property: "parity",
      displayName: "奇偶校验位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "Odd",
      enumValues: [
        "None(无校验)",
        "Odd(奇校验)",
        "Even(偶校验)",
        "Mark(1校验)",
        "Space(0校验)"
      ],
      required: true
    },
    {
      property: "address",
      displayName: "地址",
      dataType: "number",
      max: 255,
      min: 1,
      precision: 0,
      unit: null,
      defaultValue: 1,
      enumValues: null,
      required: true
    }
  ],
  ADAM4150: [
    {
      property: "baudRate",
      displayName: "波特率",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: 9600,
      enumValues: ["9600", "19200", "38400", "57600", "115200"],
      required: true
    },
    {
      property: "dataBits",
      displayName: "数据位",
      dataType: "number",
      max: 8,
      min: 5,
      precision: 0,
      unit: null,
      defaultValue: 8,
      enumValues: null,
      required: true
    },
    {
      property: "stopBits",
      displayName: "停止位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "One",
      enumValues: ["None(无)", "One(1)", "Two(2)", "OnePointFive(1.5)"],
      required: true
    },
    {
      property: "parity",
      displayName: "奇偶校验位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "Odd",
      enumValues: [
        "None(无校验)",
        "Odd(奇校验)",
        "Even(偶校验)",
        "Mark(1校验)",
        "Space(0校验)"
      ],
      required: true
    },
    {
      property: "address",
      displayName: "地址",
      dataType: "number",
      max: 255,
      min: 1,
      precision: 0,
      unit: null,
      defaultValue: 1,
      enumValues: null,
      required: true
    }
  ],
  HnwDC1000: [
    {
      property: "baudRate",
      displayName: "波特率",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: 9600,
      enumValues: ["9600", "19200", "38400", "57600", "115200"],
      required: true
    },
    {
      property: "dataBits",
      displayName: "数据位",
      dataType: "number",
      max: 8,
      min: 5,
      precision: 0,
      unit: null,
      defaultValue: 8,
      enumValues: null,
      required: true
    },
    {
      property: "stopBits",
      displayName: "停止位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "One",
      enumValues: ["None(无)", "One(1)", "Two(2)", "OnePointFive(1.5)"],
      required: true
    },
    {
      property: "parity",
      displayName: "奇偶校验位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "Odd",
      enumValues: [
        "None(无校验)",
        "Odd(奇校验)",
        "Even(偶校验)",
        "Mark(1校验)",
        "Space(0校验)"
      ],
      required: true
    },
    {
      property: "address",
      displayName: "地址",
      dataType: "number",
      max: 255,
      min: 1,
      precision: 0,
      unit: null,
      defaultValue: 1,
      enumValues: null,
      required: true
    },
    {
      property: "protocol",
      displayName: "通讯协议",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: "TAIE",
      enumValues: ["TAIE(TAIE)"],
      required: false
    }
  ],
  LORA: [],
  DTS: [],
  PRESSDUCTOR: [
    {
      property: "rangeMeasure",
      displayName: "单位",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: ["MPa", "KPa", "PSI"],
      required: true
    },
    {
      property: "rangeFrom",
      displayName: "最小量程",
      dataType: "number",
      max: 10000,
      min: 0,
      precision: 1,
      unit: "rangeMeasure:[MPa,KPa]",
      defaultValue: null,
      enumValues: null,
      required: true
    },
    {
      property: "rangeTo",
      displayName: "最大量程",
      dataType: "number",
      max: 10000,
      min: 0,
      precision: 1,
      unit: "rangeMeasure:[MPa,KPa]",
      defaultValue: null,
      enumValues: null,
      required: true
    },
    {
      property: "ratio",
      displayName: "分辨率",
      dataType: "number",
      max: 100,
      min: 0,
      precision: 0.01,
      unit: null,
      defaultValue: 0.01,
      enumValues: null,
      required: true
    },
    {
      property: "parentId&chlIdx",
      displayName: "IO模块",
      dataType: "findChannel(PRESSDUCTOR)",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: null,
      required: true
    }
  ],
  THERMOCOUPLE: [
    {
      property: "spec",
      displayName: "规格",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: [
        "TypeJ(T/C TypeJ 0~760 °C)",
        "TypeK(T/C TypeK 0~1370 °C)",
        "TypeT(T/C TypeT -100~400 °C)",
        "TypeE(T/C TypeE 0~1000 °C)",
        "TypeR(T/C TypeR 500~1570 °C)",
        "TypeS(T/C TypeS 500~1750 °C)",
        "TypeB(T/C TypeB 500~1800 °C)"
      ],
      required: true
    },
    {
      property: "parentId&chlIdx",
      displayName: "IO模块",
      dataType: "findChannel(THERMOCOUPLE,spec)",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: null,
      required: true
    }
  ],
  VALVE: [
    {
      property: "parentId&chlIdx",
      displayName: "上级设备",
      dataType: "findChannel(VALVE_OPEN_DITECT)",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: null,
      required: true
    },
    {
      property: "ctrlParentId&ctrlChlIdx",
      displayName: "开度输出",
      dataType: "findChannel(VALVE_OUTPUT_CONTROL)",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: null,
      required: true
    }
  ],
  SWITCH: [
    {
      property: "parentId&chlIdx",
      displayName: "上级设备",
      dataType: "findChannel('')",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: null,
      required: true
    }
  ],
  LED: [
    {
      property: "parentId&chlIdx",
      displayName: "上级设备",
      dataType: "findChannel('')",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: null,
      required: true
    },
    {
      property: "triggerLevel",
      displayName: "触发电平",
      dataType: "enum",
      max: null,
      min: null,
      precision: null,
      unit: null,
      defaultValue: null,
      enumValues: [0, 1],
      required: true
    }
  ]
};
export default [
  {
    url: "/deviceProperties",
    method: "get",
    response: () => {
      return {
        errorCode: 0,
        message: "",
        data: DEVICE_PROPERTIES
      };
    }
  },
  {
    url: "/deviceProperties",
    method: "post",
    response: ({ body }) => {
      // 批量添加，body是属性数组，则批量添加
      if (Array.isArray(body)) {
        for (let i = 0; i < body.length; i) {
          if (!DEVICE_PROPERTIES[body[i].model]) {
            DEVICE_PROPERTIES[body[i].model] = [];
          }
          DEVICE_PROPERTIES[body[i].model].push(body[i]);
        }
      } else {
        // 单个添加
        if (!DEVICE_PROPERTIES[body.model]) {
          DEVICE_PROPERTIES[body.model] = [];
        }
        DEVICE_PROPERTIES[body.model].push(body);
      }

      return {
        errorCode: 0,
        message: "",
        data: DEVICE_PROPERTIES
      };
    }
  },
  {
    url: "/deviceProperties",
    method: "delete",
    response: ({ body }) => {
      // 批量删除，body是属性数组，找到属性进行删除
      if (Array.isArray(body)) {
        for (let i = 0; i < body.length; i) {
          if (DEVICE_PROPERTIES[body[i].model]) {
            for (let j = 0; j < DEVICE_PROPERTIES[body[i].model].length; j) {
              if (
                DEVICE_PROPERTIES[body[i].model][j].property ===
                body[i].property
              ) {
                DEVICE_PROPERTIES[body[i].model].splice(j, 1);
                break;
              }
            }
          }
        }
      } else {
        // 单个删除
        if (DEVICE_PROPERTIES[body.model]) {
          for (let i = 0; i < DEVICE_PROPERTIES[body.model].length; i) {
            if (DEVICE_PROPERTIES[body.model][i].property === body.property) {
              DEVICE_PROPERTIES[body.model].splice(i, 1);
              break;
            }
          }
        }
      }

      return {
        errorCode: 0,
        message: "",
        data: DEVICE_PROPERTIES
      };
    }
  },
  {
    url: "/deviceProperties",
    method: "put",
    response: ({ body }) => {
      // 批量更新，body是属性数组，则批量更新
      if (Array.isArray(body)) {
        for (let i = 0; i < body.length; i) {
          if (!DEVICE_PROPERTIES[body[i].model]) {
            DEVICE_PROPERTIES[body[i].model] = [];
          }
          for (let j = 0; j < DEVICE_PROPERTIES[body[i].model].length; j) {
            if (
              DEVICE_PROPERTIES[body[i].model][j].property === body[i].property
            ) {
              DEVICE_PROPERTIES[body[i].model][j] = body[i];
            }
          }
        }
      } else {
        // 单个更新
        if (!DEVICE_PROPERTIES[body.model]) {
          DEVICE_PROPERTIES[body.model] = [];
        }
        for (let i = 0; i < DEVICE_PROPERTIES[body.model].length; i) {
          if (DEVICE_PROPERTIES[body.model][i].property === body.property) {
            DEVICE_PROPERTIES[body.model][i] = body;
          }
        }
      }

      return {
        errorCode: 0,
        message: "",
        data: DEVICE_PROPERTIES
      };
    }
  }
] as MockMethod[];
